package com.example.administrator.hqq.adapter.discover;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.example.administrator.hqq.R;
import com.example.administrator.hqq.activity.drag.PhotoActivity;
import com.example.administrator.hqq.bean.PostDetailsBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * @name: HQQ
 * @describe:
 * @author: 康海涛 QQ2541849981
 * @time: 2018/11/21 14:56
 * @remark:
 */
public class PostDetailsImageListAdapter extends RecyclerView.Adapter<PostDetailsImageListAdapter.ViewHolder> {

    private Context context;
    private List<PostDetailsBean.DataBean.PictureUrlBean> strings;
    private PicCountLogic picCountLogic;

    public void setPicCountLogic(PicCountLogic picCountLogic) {
        this.picCountLogic = picCountLogic;
    }

    public PostDetailsImageListAdapter(Context context, List<PostDetailsBean.DataBean.PictureUrlBean> strings) {
        this.context = context;
        this.strings = strings;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.post_image)
        ImageView postImage;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.post_details_list_item, null);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
        Glide.with(context).load(strings.get(position).getLarge())
                .listener(new RequestListener<String, GlideDrawable>() {
                    @Override
                    public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                        picCountLogic.countLogic();
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                        return false;
                    }
                })
                .into(new SimpleTarget<GlideDrawable>() {
                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                        holder.postImage.setImageDrawable(resource);
                        picCountLogic.countLogic();
                    }
                });
        holder.postImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ArrayList<String> string=new ArrayList<>();
                for (int i2 = 0; i2 < strings.size(); i2++) {
                    string.add(strings.get(i2).getOrigin());
                }
                PhotoActivity.start(context,string,position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return strings.size();
    }

    public interface PicCountLogic{
        void countLogic();
    }
}
